from datetime import datetime
from fastapi import APIRouter, Depends, Form, Request
from pydantic_validation_decorator import ValidateFields
from sqlalchemy.ext.asyncio import AsyncSession
from config.enums import BusinessType
from config.get_db import get_db
from module_admin.annotation.log_annotation import Log
from module_admin.aspect.interface_auth import CheckUserInterfaceAuth
from module_admin.entity.vo.user_vo import CurrentUserModel
from module_admin.service.login_service import LoginService
from module_plant.service.planter_service import PlanterService
from module_plant.entity.vo.planter_vo import DeletePlanterModel, PlanterModel, PlanterPageQueryModel
from utils.common_util import bytes2file_response
from utils.log_util import logger
from utils.page_util import PageResponseModel
from utils.response_util import ResponseUtil


planterController = APIRouter(prefix='/plant/planter', dependencies=[Depends(LoginService.get_current_user)])


@planterController.get(
    '/list', response_model=PageResponseModel, dependencies=[Depends(CheckUserInterfaceAuth('plant:planter:list'))]
)
async def get_plant_planter_list(
    request: Request,
    planter_page_query: PlanterPageQueryModel = Depends(PlanterPageQueryModel.as_query),
    query_db: AsyncSession = Depends(get_db),
):
    # 获取分页数据
    planter_page_query_result = await PlanterService.get_planter_list_services(query_db, planter_page_query, is_page=True)
    logger.info('获取成功')

    return ResponseUtil.success(model_content=planter_page_query_result)


@planterController.post('', dependencies=[Depends(CheckUserInterfaceAuth('plant:planter:add'))])
@ValidateFields(validate_model='add_planter')
@Log(title='种植主体信息', business_type=BusinessType.INSERT)
async def add_plant_planter(
    request: Request,
    add_planter: PlanterModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    add_planter.create_by = current_user.user.user_name
    add_planter.update_by = current_user.user.user_name
    add_planter.year = datetime.now().year
    add_planter_result = await PlanterService.add_planter_services(query_db, add_planter)
    logger.info(add_planter_result.message)

    return ResponseUtil.success(msg=add_planter_result.message)


@planterController.put('', dependencies=[Depends(CheckUserInterfaceAuth('plant:planter:edit'))])
@ValidateFields(validate_model='edit_planter')
@Log(title='种植主体信息', business_type=BusinessType.UPDATE)
async def edit_plant_planter(
    request: Request,
    edit_planter: PlanterModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    edit_planter.update_by = current_user.user.user_name
    edit_planter.update_time = datetime.now()
    edit_planter_result = await PlanterService.edit_planter_services(query_db, edit_planter)
    logger.info(edit_planter_result.message)

    return ResponseUtil.success(msg=edit_planter_result.message)


@planterController.delete('/{planter_ids}', dependencies=[Depends(CheckUserInterfaceAuth('plant:planter:remove'))])
@Log(title='种植主体信息', business_type=BusinessType.DELETE)
async def delete_plant_planter(request: Request, planter_ids: str, query_db: AsyncSession = Depends(get_db)):
    delete_planter = DeletePlanterModel(planterIds=planter_ids)
    delete_planter_result = await PlanterService.delete_planter_services(query_db, delete_planter)
    logger.info(delete_planter_result.message)

    return ResponseUtil.success(msg=delete_planter_result.message)


@planterController.get(
    '/{planter_id}', response_model=PlanterModel, dependencies=[Depends(CheckUserInterfaceAuth('plant:planter:query'))]
)
async def query_detail_plant_planter(request: Request, planter_id: int, query_db: AsyncSession = Depends(get_db)):
    planter_detail_result = await PlanterService.planter_detail_services(query_db, planter_id)
    logger.info(f'获取planter_id为{planter_id}的信息成功')

    return ResponseUtil.success(data=planter_detail_result)


@planterController.post('/export', dependencies=[Depends(CheckUserInterfaceAuth('plant:planter:export'))])
@Log(title='种植主体信息', business_type=BusinessType.EXPORT)
async def export_plant_planter_list(
    request: Request,
    planter_page_query: PlanterPageQueryModel = Form(),
    query_db: AsyncSession = Depends(get_db),
):
    # 获取全量数据
    planter_query_result = await PlanterService.get_planter_list_services(query_db, planter_page_query, is_page=False)
    planter_export_result = await PlanterService.export_planter_list_services(planter_query_result)
    logger.info('导出成功')

    return ResponseUtil.streaming(data=bytes2file_response(planter_export_result))
